package com.bupt.ilink.config;

import com.bupt.ilink.jwt.JwtAuthenticationEntryPoint;
import com.bupt.ilink.jwt.JwtTokenAuthenticationFilter;
import com.bupt.ilink.jwt.JwtTokenProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import org.springframework.http.HttpMethod;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.web.cors.CorsUtils;

@Configuration
@EnableWebSecurity
public class SecurityConfig {

    @Lazy
    @Autowired
    JwtTokenProvider jwtTokenProvider;

    @Bean
    public AuthenticationManager authenticationManagerBean(
            AuthenticationConfiguration authenticationConfiguration) throws Exception {
        return authenticationConfiguration.getAuthenticationManager();
    }

    /**
     * 对密码进行编码
     *
     * @return org.springframework.security.crypto.password.PasswordEncoder
     * @author liuzheng
     * @create 2024-07-27
     **/
    @Bean
    PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    /**
     * 过滤器链
     *
     * @param http
     * @return org.springframework.security.web.SecurityFilterChain
     * @author liuzheng
     * @create 2024-07-27
     **/
    @Bean
    protected SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
        JwtTokenAuthenticationFilter customFilter = new JwtTokenAuthenticationFilter(jwtTokenProvider);

        http
                .httpBasic(httpBasic -> httpBasic.disable()) // 禁用HTTP基本认证
                .csrf(csrf -> csrf.disable()) // 禁用csrf保护
                .sessionManagement(sessionManagement -> sessionManagement // 配置会话管理策略，所有请求不使用会话状态
                        .sessionCreationPolicy(SessionCreationPolicy.STATELESS))
                .authorizeRequests(authorize -> authorize
                                .requestMatchers("/user/login").permitAll()
                                .requestMatchers("/user/register").permitAll() // 登录页和注册页允许所有用户访问
                                .requestMatchers("/user/school_login").permitAll() // 登录页和注册页允许所有用户访问
                                .requestMatchers("/user/personal_register").permitAll() // 登录页和注册页允许所有用户访问
                                .requestMatchers("/user/refresh").permitAll()
                                .requestMatchers("/user/publicKey").permitAll()
//                        .requestMatchers(HttpMethod.GET, "/admin").hasRole("ADMIN") // 定义一个只允许管理员访问的页面
                                .requestMatchers(HttpMethod.GET, "/user").hasAuthority("read") // 只允许用户访问的页面
                                .requestMatchers(HttpMethod.GET, "/admin").hasAuthority("write")
                                .requestMatchers(HttpMethod.PUT, "/*").authenticated()
                                .anyRequest()
                                .authenticated()
                )
                .exceptionHandling(exceptionHandling ->
                        exceptionHandling.authenticationEntryPoint(new JwtAuthenticationEntryPoint())
                )
                .addFilterBefore(customFilter, UsernamePasswordAuthenticationFilter.class); // 确定自定义过滤器的位置

        return http.build();
    }

}
